package com.karry.util;

import com.karry.mapper.RecordMapper;
import com.karry.service.RecordService;
import com.karry.service.impl.RecordServiceImpl;
import com.karry.service.impl.UserServiceImpl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.http.HttpServletRequest;

public class RecordUtil {

    private static SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    //专注登录信息
    public static void insertRecord(int status, int id, HttpServletRequest request){
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        RecordMapper recordMapper = sqlSession.getMapper(RecordMapper.class);
        String[] ipAddr = UserLoginInfo.getIpAddr(request);
        String s = "";
        if (status == 1){
            s = "登录成功";
            //修改登录记录
            new UserServiceImpl().updateLoginInfo(id, ipAddr[1]);
        }else {
            s = "登录失败";
        }

        recordMapper.insertRecord(s, id, -1, ipAddr[0], ipAddr[1]);
        sqlSession.close();
    }

}
